/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package ext.tianma.part.finishedProduct.builder;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.jca.mvc.components.JcaTreeConfig;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.ComponentResultProcessor;
import com.ptc.mvc.components.TreeConfig;
import com.ptc.mvc.components.TreeDataBuilderAsync;
import com.ptc.mvc.components.TreeNode;
import com.ptc.mvc.components.ds.DataSourceMode;
import com.ptc.mvc.util.ClientMessage;
import com.ptc.netmarkets.util.beans.NmHelperBean;
import com.ptc.windchill.enterprise.classification.ClfPickerTreeHandler;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import wt.util.WTException;

@ComponentBuilder({ "classification.mvc.custClassificationPickerTreeBuilder" })
public class CustClassificationPickerTreeBuilder extends AbstractComponentBuilder
		implements TreeDataBuilderAsync {
	private static final String RESOURCE = "com.ptc.windchill.enterprise.classification.classificationResource";
	private ClfPickerTreeHandler treeHandler;
	public static final String CLASSIFICATION_SELECTABLE_DIRECTORIES_PROPERTY = "SelectableDirectories";
	public static final String CLASSIFICATION_SELECTABLE_DIRECTORIES_ALL = "All";

	public CustClassificationPickerTreeBuilder() {
		this.treeHandler = null;
	}

	public Object buildComponentData(ComponentConfig paramComponentConfig,
			ComponentParams paramComponentParams) throws Exception {
		return new ClfPickerTreeHandler();
	}

	public ComponentConfig buildComponentConfig(
			ComponentParams paramComponentParams) throws WTException {
		JcaComponentParams localJcaComponentParams = (JcaComponentParams) paramComponentParams;
		NmHelperBean localNmHelperBean = localJcaComponentParams
				.getHelperBean();
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) localNmHelperBean
				.getRequest();

		ComponentConfigFactory localComponentConfigFactory = getComponentConfigFactory();
		TreeConfig localTreeConfig = localComponentConfigFactory
				.newTreeConfig();
		localTreeConfig.setNodeColumn("clf");
		localTreeConfig.setId("clfPicker.tree");
		localTreeConfig
				.setLabel(ClientMessage
						.get("com.ptc.windchill.enterprise.classification.classificationResource",
								"CLASSIFICATION_PICKER_TITLE"));

		localTreeConfig.setExpansionLevel("one");
		localTreeConfig.setActionModel("classification picker toolbar");
		localTreeConfig.setSelectable(true);

		((JcaTreeConfig) localTreeConfig).setDescriptorProperty(
				"SelectableDirectories",
				localHttpServletRequest.getParameter("SelectableDirectories"));

		ColumnConfig localColumnConfig1 = localComponentConfigFactory
				.newColumnConfig(
						"clf",
						ClientMessage
								.get("com.ptc.windchill.enterprise.classification.classificationResource",
										"CLASSIFICATION_COLUMN_TITLE"), true);
		localColumnConfig1.setDefaultSort(true);
		localTreeConfig.addComponent(localColumnConfig1);

		ColumnConfig localColumnConfig2 = localComponentConfigFactory
				.newColumnConfig("clfPickerNonSelectableColumn", false);
		localColumnConfig2.setDataStoreOnly(true);
		localTreeConfig.addComponent(localColumnConfig2);
		localTreeConfig.setNonSelectableColumn(localColumnConfig2);

		((JcaTreeConfig) localTreeConfig)
				.setDataSourceMode(DataSourceMode.ASYNCHRONOUS);
		return localTreeConfig;
	}

	public void buildNodeData(Object paramObject,
			ComponentResultProcessor paramComponentResultProcessor)
			throws Exception {
		Boolean localBoolean = Boolean.FALSE;
		Object localObject1 = ((JcaTreeConfig) paramComponentResultProcessor
				.getConfig()).getDescriptorProperty("SelectableDirectories");
		if ("All".equals(localObject1))
			localBoolean = Boolean.TRUE;
		Map localMap;
		Iterator localIterator;
		if (paramObject == TreeNode.RootNode) {
			this.treeHandler = new ClfPickerTreeHandler();
			this.treeHandler.setSelectableDirectories(localBoolean);
			paramComponentResultProcessor.addElements(this.treeHandler
					.getRootNodes());
		} else {
			ArrayList localArrayList = new ArrayList();
			localArrayList.add(paramObject);
			localMap = this.treeHandler.getNodes(localArrayList);
			Set localSet = localMap.keySet();
			for (localIterator = localSet.iterator(); localIterator.hasNext();) {
				Object localObject2 = localIterator.next();
				paramComponentResultProcessor.addElements((Iterable) localMap
						.get(localObject2));
			}
		}
	}
}